package com.zhuozhou.jdyc.wight.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhuozhou.jdyc.R;

/**
 * Created by 方舟 on 2017/11/2.
 * 自定义加载dialog
 */

public class CustomProgressDialog extends Dialog {
    private static CustomProgressDialog customProgressDialog;
    private TextView tipTextView = null;
    private ImageView spaceshipImage = null;
    private LinearLayout llProgress;
    private boolean isCanCancel = false;
    private Context context;
    private OnCancelListener onCancelListener;
    /**
     * 是否展示加载背景
     */
    private boolean isShow;
    /**
     * 加载提示文字
     */
    private String message = "正在加载,请稍后...";

    public CustomProgressDialog(Context context) {
        super(context, R.style.loading_dialog);
        this.context = context;
    }

    public static CustomProgressDialog getInstance(Context context) {
        if (customProgressDialog == null || !customProgressDialog.isShowing()) {
            synchronized (context.getClass()) {
                customProgressDialog = new CustomProgressDialog(context);
            }
        }
        return customProgressDialog;
    }

    public CustomProgressDialog(Context context, int theme) {
        super(context, theme);
        this.context = context;
    }

    public CustomProgressDialog setCanCancel(boolean isCanCancel) {
        this.isCanCancel = isCanCancel;
        return this;
    }

    public void refreshCanCancel(boolean isCanCancel) {
        this.isCanCancel = isCanCancel;
        setCancelable(isCanCancel);
    }
    public CustomProgressDialog setCanCelListener(OnCancelListener onCancelListener){
        this.onCancelListener = onCancelListener;
        return this;
    }

    public CustomProgressDialog builder() {
        createLoadingDialog(context);
        return this;
    }

    private void createLoadingDialog(Context context) {
        View v = LayoutInflater.from(context).inflate(R.layout.loading_dialog, null);

        llProgress = v.findViewById(R.id.ll_progress);
        spaceshipImage = v.findViewById(R.id.img);
        tipTextView = v.findViewById(R.id.tipTextView);
        tipTextView.setText(message);
        if (isShow) {
            llProgress.setBackground(null);
        } else {
            llProgress.setBackgroundResource(R.drawable.round_prodialog);
        }
        Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(
                context, R.anim.load_animation);
        spaceshipImage.startAnimation(hyperspaceJumpAnimation);

        setCanceledOnTouchOutside(false);
        setCancelable(isCanCancel);
        setOnCancelListener(onCancelListener);
        setContentView(v, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
    }

    public CustomProgressDialog isShowBg(boolean isShow) {
        this.isShow = isShow;
        return this;
    }

    public CustomProgressDialog setMessage(String message) {
        this.message = message;
        return this;
    }

    public void setImgResource(int imgId) {
        if (spaceshipImage != null) {
            spaceshipImage.setImageResource(imgId);
        }
    }

}
